<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Collect\CollectController;
use EasyWeChat\Kernel\Messages\News;
use EasyWeChat\Kernel\Messages\NewsItem;
use Illuminate\Support\Facades\Log;

class WechatController extends CollectController
{

    /**
     * 处理微信的请求消息
     *
     * @return string
     */
    public function serve()
    {
        Log::info('request arrived.'); # 注意：Log 为 Laravel 组件，所以它记的日志去 Laravel 日志看
        $app = app('wechat.official_account');
        $app->server->push(function ($message) {
            switch ($message['MsgType']) {
                case 'text':
                    return $this->Info($message['Content']);
                    break;
                case 'event':
                    return config('wx.gzresp');
                    break;
            }
        });
        return $app->server->serve();
    }

    public function menu_add(){
        $app = app('wechat.official_account');
        $dianying = $this->Common()->shorturl("http://".config('wx.domin')."/videolist/dianying-all-1.html");
        $dianshi = $this->Common()->shorturl("http://".config('wx.domin')."/videolist/dianshi-all-1.html");
        $zongyi = $this->Common()->shorturl("http://".config('wx.domin')."/videolist/zongyi-all-1.html");
        $dongman = $this->Common()->shorturl("http://".config('wx.domin')."/videolist/dongman-all-1.html");
        $buttons = [
            [
                "type" => "view",
                "name" => "首页",
                "url"  => "http://".config('wx.domin')
            ],
            [
                "name"       => "分类",
                "sub_button" => [
                    [
                        "type" => "view",
                        "name" => "电影",
                        "url"  => $dianying,
                    ],
                    [
                        "type" => "view",
                        "name" => "电视",
                        "url"  => $dianshi,
                    ],
                    [
                        "type" => "view",
                        "name" => "综艺",
                        "url" => $zongyi,
                    ],
                    [
                        "type" => "view",
                        "name" => "动漫",
                        "url" => $dongman,
                    ],
                ],
            ],
        ];
       return $app->menu->create($buttons);
    }

    private function Info($key)
    {
        $arr = $this->Search($key);
        if (empty($arr)) {
            $info['title'] = config('wx.title');
            $info['image'] = config('wx.img');
            $info['description'] = config('wx.description');
            $info['url'] = config('wx.url');
        } else {
            $info['title'] = $arr[0]['title'];
            $info['image'] = $arr[0]['img'];
            $info['description'] = config('wx.description');
            if (config('wx.shorturl') == 1) {
                $info['url'] = $this->Common()->shorturl('http://' . config('wx.domin') . '/chazhao?cz=' . $key);
            } else {
                $info['url'] = 'http://' . config('wx.domin') . '/chazhao?cz=' . $key;
            }
        }
        $items = new NewsItem($info);
        $news = new News([$items]);
        return $news;
    }


}